package com.itheima.riggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.riggie.common.R;
import com.itheima.riggie.domain.Employee;
import com.itheima.riggie.service.EmployeeService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){
        //1将密码进行加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes()); //加密
        //2,判断用户名是否存在

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //查询数据库是否有跟页面传过来的username相等的
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //3判断查询的数据是否为空
        if (emp==null){
            return R.error("登陆失败");        }
        //4密码比对,比对的是查询到username的密码
        if (!emp.getPassword().equals(password)){
            return R.error("登陆失败");
        }
        //5查看员工状态是否为禁用状态
        //程序走到这,已经登录成功,可以获取到该员工的其他数据
        if (emp.getStatus()==0){
            return R.error("账号已被禁用");
        }
        //登录成功,将员工的id存入session当中,并返回登录成功
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        // 清理
        session.removeAttribute("employee");
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工,员工信息: {}",employee.toString());
        //设置初始密码123456,需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*
        * 类似日志   记录修改人 修改时间 现在使用拦截器统一管理
        * */
//        employee.setCreateTime(LocalDateTime.now()); //创建时间
//        employee.setUpdateTime(LocalDateTime.now());  //修改时间

//
//        //获得当前登录用户的id
//       Long empId=(Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);  //创建人
//        employee.setUpdateUser(empId);   //修改人

        employeeService.save(employee);
        return R.success("新增员工成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page= {},pageSize= {},name= {}",page,pageSize,name);
        Page pageInfo = new Page(page,pageSize);

        //条件查询
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //获取登录用户id
//        Long employeeId = (Long) request.getSession().getAttribute("employee");
        //更新修改人
//        employee.setUpdateUser(employeeId);
        //更新修改时间
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改员工信息成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }



































}
